<template>
  <div class="goods">
    <div class="menu-wrapper" ref= 'menuWrapper'>
      <ul>
        <li v-for ='(item,index) in goods' class="menu-item" :class ="{ current: currentIndex === index }"
            @click="selectMenu(index,$event)">
          <span class="text">
            <icons v-if = 'item.type > 0' :type = 'item.type'></icons>
            {{item.name}}
          </span>
        </li>
      </ul>
    </div>
    <div class="foods-wrapper" ref= 'foodsWrapper'>
      <ul>
        <li v-for = 'item in goods' class="food-list food-list-hook">
          <h1 class="title">{{item.name}}</h1>
          <ul>
            <li v-for ='food in item.foods' class="food-item">
              <div class="icon">
                <img :src = 'food.icon'>
              </div>
              <div class="cotent">
                <h2 class="name">{{food.name}}</h2>
                <p class="desc">{{food.description}}</p>
                <div class="extra">
                  <span>月售{{food.sellCount}}份</span>
                  <span>好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="new">￥{{food.price}}</span>
                  <span v-show ='food.oldPrice' class="old">￥{{food.oldPrice}}</span>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <shopCar></shopCar>
  </div>
</template>
<script>
  import icons from 'components/icon/icon';
  import BScroll from 'better-scroll';
  import shopCar from 'components/showcar/showcar';
  export default {
    props: {
      seller: {
        type: Object
      }
    },
    data() {
      return {
        goods: [],
        listHeight: [],
        scrollY: 0
      };
    },
    computed: {
      currentIndex: function () {
        for (let i = 0; i < this.listHeight.length; i++) {
          let height1 = this.listHeight[i];
          let height2 = this.listHeight[i + 1];
          if (!height2 || this.scrollY >= height1 && this.scrollY < height2) {
            return i;
          }
        }
        return 0;
      }
    },
    created() {
      var ERR_OK = 0;
      this.$http.get('http://192.168.10.150:8080');
      this.$http.get('api/goods').then((response) => {
        response = response.body;
        if (response.errno === ERR_OK) {
          this.goods = response.data;
          this.$nextTick(() => {
            this._initScroll();
            this._calculateHeight();
          });
        }
      });
    },
    components: {
      icons,
      shopCar
    },
    methods: {
      _initScroll() {
        this.menuScroll = new BScroll(this.$refs.menuWrapper, {
          click: true
        });
        this.foodScroll = new BScroll(this.$refs.foodsWrapper, {
          probeType: 3
        });
        this.foodScroll.on('scroll', (pos) => {
          this.scrollY = Math.abs(Math.round(pos.y));
        });
      },
      _calculateHeight() {
        let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook');
        let height = 0;
        this.listHeight.push(height);
        for (let i = 0; i < foodList.length; i++) {
          let item = foodList[i];
          height += item.clientHeight;
          this.listHeight.push(height);
        }
      },
      selectMenu(index, event) {
        if (!event._constructed) {
          return;
        };
        let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook');
        let el = foodList[index];
        this.currentIndex = index;
        this.foodScroll.scrollToElement(el, 300);
      }
    }

  };
</script>
<style lang="scss" rel="stylesheet/scss">
  @import "../../common/scss/mixin.scss";
  .goods{
    position: absolute;
    top:187px;
    bottom: 46px;
    width: 100%;
    overflow: hidden;
    .menu-wrapper{
      float: left;
      width: 20%;
      height:100%;
      background: #f3f5f7;
      .menu-item{
        display: table;
        width: 100%;
        text-align: center;
        height: 54px;
        line-height: 14px;
        @include border-1px(rgba(7,17,27,0.1));
        &.current{
          position: relative;
          margin-top: -1px;
          z-index: 10;
          background: #fff;
          font-weight: 700;
          border:0px;
        }
        .text{
          display: table-cell;
          vertical-align: middle;
          font-size: 12px;
        }
      }

    }
    .foods-wrapper{
      float: left;
      width: 80%;
      height:100%;
      .title{
        margin: 0px;
        height:26px;
        line-height: 26px;
        border-left:2px solid #d9dde1;
        color: rgb(147,153,159);
        font-size: 12px;
        background: #f3f5f7;
        font-weight: bold;
      }
      .food-item{
        height: 100%;
        @include clear();
        @include border-1px(rgba(7,17,27,0.1));
        padding: 20px 0px;
        .icon{
          width: 40%;
          float: left;
          text-align: center;
          img{
            width: 50%;
          }
        }
        .cotent{
          width:60%;
          float: left;
          color: rgb(147,153,159);
          .name{
            color: #000;
            margin-bottom: 10px;
          }
          .desc{
            margin-bottom: 10px;
          }
          .extra{
            margin-bottom: 10px;
          }
          .price{
            font-weight: 700;
            line-height: 24px;
            .new{
              margin-right: 8px;
              font-size: 14px;
              color:rgb(240,20,20);
            }
            .old{
              text-decoration: line-through;
              font-size: 10px;
              color: rgb(147,153,159);
            }
          }
        }
      }
    }
  }
</style>
